package com.card.framework.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.context.ServletContextAware;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.manager.dao.MsaMapper;
import com.card.manager.domain.Msa;
import com.card.manager.domain.MsaExample;


public class InitDataCode extends MyFrameworkSupperMapper implements ServletContextAware{
	private  ServletContext context;
	public static final Map <String,JSONArray> codeMap = new HashMap<String,JSONArray>();
	public static final Map <String,JSONObject> codeJson = new HashMap<String,JSONObject>();
	public void initDataCode(){
		MsaMapper msaMapper = this.getMapper(MsaMapper.class);
		MsaExample example = new MsaExample();
		example.setOrderByClause("kindid , sort asc");
		example.createCriteria().andDelflgEqualTo((byte)1);
		List<Msa> msaList = msaMapper.selectByExample(example);
		JSONArray arr = new JSONArray();
		JSONObject myjson = new JSONObject();
		String temp="";
		for(Msa msa : msaList){
			if(!temp.equals(msa.getKindid())&&arr.size()!=0){
				codeMap.put(temp, arr);
				codeJson.put(temp, myjson);
				arr = new JSONArray();
				myjson = new JSONObject();
			}
			JSONObject json = new JSONObject();
			json.accumulate("label", msa.getId().toString());
			json.put("value", msa.getNamea());
			myjson.put(msa.getId().toString(), msa.getReserve01()!=null&&!"".equals(msa.getReserve01())?msa.getNamea()+"-"+msa.getReserve01():msa.getNamea());
			arr.add(json);
			temp= msa.getKindid();
		}
		codeMap.put(temp, arr);
		codeJson.put(temp, myjson);
		context.setAttribute("code", codeMap);
		context.setAttribute("json", codeJson);
	}
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.context=servletContext;
	}
}
